unit Delphi_Dialog;

interface
uses
  SysUtils, Windows,Classes, Controls, Dialogs, PythonEngine, WrapDelphi;
type
  TPyDelphiDialogWrapper = class(TPyDelphiWrapper)
  private
    function InputQuery_Wrap(pself, args: PPyObject): PPyObject; cdecl;
    function CheckListQuery_Wrap(pself, args: PPyObject): PPyObject; cdecl;
    function RadioListQuery_Wrap(pself, args: PPyObject): PPyObject; cdecl;
  public
    constructor Create( AOwner : TComponent ); override;
    procedure CreateModuleFunctions; override;
  end;
implementation

uses UFrmCheckListQuery, UFrmRadioListQuery;
//

{ TPyDelphiDialogWrapper }

function TPyDelphiDialogWrapper.CheckListQuery_Wrap(pself,
  args: PPyObject): PPyObject;
var
  _obj, _item: PPyObject;
  pCaption: PChar;
  i: Integer;
begin
  Result := nil;
  CheckEngine;
  with Engine do
  begin
    Result := Py_None;
    if PyArg_ParseTuple(args, 'sO', [@pCaption, @_obj]) <> 0 then
    begin
      with TFrmCheckListQuery.Create(nil) do
      begin
        try
          Caption := StrPas(pCaption);
          chklst.Clear;
          if not PyList_Check(_obj) then
            raise EPythonError.Create('the python object is not a list');
          for i := 0 to PyList_Size(_obj) - 1 do
          begin
            _item := PyList_GetItem(_obj, i);
            if PyString_Check(_item) then
              chklst.Items.Add(PyObjectAsString(_item));
            if PyList_Check(_item) then
            begin
              chklst.Items.Add(PyObjectAsString(PyList_GetItem(_item, 0)));
              chklst.Checked[chklst.Count - 1] := PyObjectAsString(PyList_GetItem(_item, 1)) = '1';
            end;
          end;
          ShowModal;
          if ModalResult <> mrOK then
          begin
            Result := Py_None;
            Exit;
          end;
          Result := PyList_New(0);
          for i := 0 to chklst.Items.Count - 1 do
          begin
            if chklst.Checked[i] then
              PyList_Append(Result, PPyObject(Py_True))
            else
              PyList_Append(Result, PPyObject(Py_False));
          end;
        finally
          Free;
        end;
      end;
    end;
  end;
end;

constructor TPyDelphiDialogWrapper.Create(AOwner: TComponent);
begin
  inherited;

end;

procedure TPyDelphiDialogWrapper.CreateModuleFunctions;
begin
  inherited;
    //LHQ 2010-05-20
  if Assigned(FModule) then
  begin
    RegisterFunction(PChar('InputDialog'), InputQuery_Wrap, PChar('InputQuery(Caption,Prompt,Value)'#10 + 'Hint Input User Data '));
    RegisterFunction(PChar('CheckListDialog'), CheckListQuery_Wrap, PChar('CheckListQuery(List)'#10 + 'Checklist Window '));
    RegisterFunction(PChar('RadioListDialog'), RadioListQuery_Wrap, PChar('RadioListQuery(List)'#10 + 'RadioListQuery Window '));
  end;
end;

function TPyDelphiDialogWrapper.InputQuery_Wrap(pself,
  args: PPyObject): PPyObject;
var
  pCaption, pPrompt, pValue: PChar;
  s: string;
begin
  Result := nil;
  CheckEngine;
  with Engine do
  begin
    Result := Py_None;
    if PyArg_ParseTuple(args, 'sss', [@pCaption, @pPrompt, @pValue]) <> 0 then
    begin
      s := StrPas(pValue);
      if InputQuery(pCaption, pPrompt, s) then
        Result := Py_BuildValue('s', [PChar(s)]);
    end;
  end;
end;

function TPyDelphiDialogWrapper.RadioListQuery_Wrap(pself,
  args: PPyObject): PPyObject;
var
  _obj: PPyObject;
  pCaption: PChar;
  i, idx: Integer;
begin
  Result := nil;
  CheckEngine;
  with Engine do
  begin
    Result := Py_None;
    if PyArg_ParseTuple(args, 'siO', [@pCaption, @idx, @_obj]) <> 0 then
    begin
      with TFrmRadioListQuery.Create(nil) do
      begin
        try
          Caption := StrPas(pCaption);
          rg.Items.Clear;
          for i := 0 to PyList_Size(_obj) - 1 do
            rg.Items.Add(PyObjectAsString(PyList_GetItem(_obj, i)));
          if (idx > -1) and (idx < rg.Items.Count) then
            rg.ItemIndex := idx;
          ShowModal;
          if ModalResult <> mrOK then
          begin
            Result := Py_None;
            Exit;
          end;
          Result := Py_BuildValue('i', [rg.ItemIndex]);
        finally
          Free;
        end;
      end;
    end else
      if not PyList_Check(_obj) then
        raise EPythonError.Create('the python object is not a list');
  end;

end;

end.

